package com.caiyi.financial.nirvana.sms.service;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsRegisterUser;
import com.caiyi.financial.nirvana.sms.entity.SmsUser;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.mapper.SmsRegisterUserMapper;
import com.caiyi.financial.nirvana.sms.util.*;
import com.youyu.common.api.Result;
import com.youyu.common.enums.BaseResultCode;
import com.youyu.common.exception.BizException;
import com.youyu.common.utils.YyAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class SmsRegisterUserService extends BaseService<SmsRegisterUser> {


    private static final String LOGIN_ACCOUNT = "login";

    @Autowired
    private SmsRegisterUserMapper smsRegisterUserMapper;

    @Autowired
    private Environment env;

    public void addRegisterUser(SmsRegisterUser registerUser, HttpSession session) {
        YyAssert.isFalse(ValidateUtil.isLength(registerUser.getCompanyName(), 50), ReturnCodeEnum.PARAMS_ERROR.code, "公司名称不能超过50个字符");
        YyAssert.isFalse(ValidateUtil.isLength(registerUser.getName(), 10), ReturnCodeEnum.PARAMS_ERROR.code, "姓名不能超过50个字符");
        Map param = (Map) session.getAttribute(Constant.SESSION_CUSTOMER_CODE);
        YyAssert.isFalse(null == param, ReturnCodeEnum.PARAMS_ERROR.code, "验证码已过期");
        String phone = param.get("phone") + "";
        String code = param.get("code") + "";
        int count = smsRegisterUserMapper.countPhone(phone);
        YyAssert.isFalse(count > 0, ReturnCodeEnum.PARAMS_ERROR.code, "手机号码已被注册");
        YyAssert.isFalse(!registerUser.getValidateCode().equals(code), ReturnCodeEnum.FAILED.code, "验证码不正确");
        registerUser.setPhone(phone);
        registerUser.setCreateTime(new Date());
        insertByEntitySelective(registerUser);

    }


    public void sendCode(String phone, HttpSession session) {
        YyAssert.isFalse(ValidateUtil.isNotNll(phone) && !ValidateUtil.isPhone(phone), ReturnCodeEnum.PARAMS_ERROR.code, "电话号码格式有误");
        String sendUrl = CommUtil.getSendUrl(env.getActiveProfiles()[0]);
        String code = (int) ((Math.random() * 9 + 1) * 100000) + "";
        Map params = new HashMap();
        params.put("account", LOGIN_ACCOUNT);
        params.put("content", String.format(Constant.SMS_LOGIN_REGISTER_CONTENT, code));
        params.put("phones", phone);
        String userJson = JedisService.get(String.format(JedisKey.SMS_USER, LOGIN_ACCOUNT));
        SmsUser apiUser = JSON.parseObject(userJson, SmsUser.class);
        String sign = SignUtil.createSign(params, apiUser.getPassword());
        params.put("sign", sign);
        String content = HttpClientUtil.doPost(sendUrl, JSON.toJSONString(params));
        logger.info("SmsRegisterUserService request {} - {}, response {}", sendUrl, params, content);
        Map resultMap = JSON.parseObject(content, Map.class);
        YyAssert.isFalse(!ReturnCodeEnum.SUCCESS.getCode().equals(resultMap.get("code")), ReturnCodeEnum.FAILED.code, "短信发送失败");
        Map map = new HashMap();
        map.put("phone", phone);
        map.put("code", code);
        session.setAttribute(Constant.SESSION_CUSTOMER_CODE, map);
    }
}
